﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vowei.Core;
using System.Globalization;
using System.Linq.Expressions;
using System.ComponentModel.DataAnnotations;
using System.Resources;

namespace Vowei.Web
{
    public class AspTranslator : ITranslator
    {
        public CultureInfo Culture
        {
            get;
            private set;
        }

        public AspTranslator(CultureInfo culture)
        {
            Culture = culture;
        }
        
        public string T(string message)
        {
            var obj = HttpContext.GetGlobalResourceObject("vowei", message, Culture);
            var translated = obj == null ? null : obj.ToString();
            if (string.IsNullOrEmpty(translated))
                return message;
            else
                return translated;
        }

        public string T(DateTime value)
        {
            return value.ToString(Culture);
        }

        public string T(double value)
        {
            return value.ToString(Culture);
        }

        public string T<U, TProperty>(Expression<Func<U, TProperty>> expression)
        {
            return Translator.T<U, TProperty>(this, expression);
        }

        public static AspTranslator CreateFrom(HttpRequestBase request)
        {
            var culture = CultureInfo.CurrentUICulture;

            if (request != null &&
                request.Headers["Accept-Language"] != null)
            {
                var acceptlan = request.Headers["Accept-Language"];
                var lan = acceptlan.Split(',')[0];
                culture = new CultureInfo(lan);
            }

            return new AspTranslator(culture);
        }
    }

    public class G18nController : Controller
    {
        private ITranslator _translator;
        protected ITranslator Translator
        {
            get
            {
                if (_translator == null)
                    _translator = AspTranslator.CreateFrom(Request);

                return _translator;
            }
        }

        public string T(string message)
        {
            return Translator.T(message);
        }

        public string T(DateTime value)
        {
            return Translator.T(value);
        }

        public string T(double value)
        {
            return Translator.T(value);
        }
    }

    public abstract class G18nWebViewPage : WebViewPage
    {
        private ITranslator _translator;
        private ITranslator Translator
        {
            get
            {
                if (_translator == null)
                    _translator = AspTranslator.CreateFrom(Request);

                return _translator;
            }
        }

        public string T(string message)
        {
            return Translator.T(message);
        }

        public string T(DateTime value)
        {
            return Translator.T(value);
        }

        public string T(double value)
        {
            return Translator.T(value);
        }
    }

    public abstract class G18nWebViewPage<U> : WebViewPage<U>
    {
        private ITranslator _translator;
        // 设置为protected只是为了在单元测试里面用。
        protected ITranslator Translator
        {
            get
            {
                if (_translator == null)
                    _translator = AspTranslator.CreateFrom(Request);

                return _translator;
            }
            set
            {
                _translator = value;
            }
        }

        public string T(string message)
        {
            return Translator.T(message);
        }

        public string T(DateTime value)
        {
            return Translator.T(value);
        }

        public string T(double value)
        {
            return Translator.T(value);
        }

        public string T(DateTime? value)
        {
            if (value.HasValue)
                return T(value.Value);
            else
                return null;
        }

        public string T(double? value)
        {
            if (value.HasValue)
                return T(value.Value);
            else
                return null;
        }

        public string T<TProperty>(Expression<Func<U, TProperty>> expression)
        {
            return Translator.T<U, TProperty>(expression);
        }
    }
}